<?php if (!defined('NOVA_RUN')) die('You can\'t access this file directly!');

/*
-------------------------------------------------------
*
*   NovaBoard 2
*   Copyright © 2011 NovaBoard team
*
*--------------------------------------------------------
*
*   Official site: www.novaboard.net
*   Contact e-mail: support@novaboard.net
*
*   GNU General Public License, version 2:
*   http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
*
---------------------------------------------------------
*/

class Category_Model extends Model {

	public function __construct()
	{
		parent::__construct();
	}

	// gets children
	public function getCategories($parent_id = 0)
	{
		if (!Cache::check('category_children_'.$parent_id))
		{
			$stmt = db::instance()->select('getCategories', array
			(
				'fields'	=> '*',
				'tables'	=> 'categories',
				'where'		=> array('parent_id' => array('=', '?')),
				'order_by'	=> array(array('order', 'asc'))
			));
			$stmt->execute(array($parent_id));
			$cats = $stmt->fetchAll();
			
			// save
			Cache::save('category_children_'.$parent_id, $cats);
		}
		else
		{
			$cats = Cache::load('category_children_'.$parent_id);
		}

		return $cats;
	}

	public function checkCategoryChildren($parent_id)
	{
		if (!Cache::check('category_children_'.$parent_id))
		{
			$stmt = db::instance()->select('getCategories', array
			(
				'fields'	=> '*',
				'tables'	=> 'categories',
				'where'		=> array('parent_id' => array('=', '?')),
				'order_by'	=> array(array('order', 'asc'))
			));
			$stmt->execute(array($parent_id));
			$cats = $stmt->fetchAll();
			
			// save
			Cache::save('category_children_'.$parent_id, $cats);
			return (sizeof($cats) > 0) ? true : false;
		}
		else
		{
			return true;
		}
	}

	public function getFullCatTree($parent_id=0, $group_id)
	{
		$cats = $this->getCategories($parent_id);
		foreach ($cats as &$cat)
		{
			$cat['permissions'] = $this->getCategoryPermissions($cat['id'], $group_id);
			if ($this->checkCategoryChildren($cat['id']))
				$cat['children'] = $this->getFullCatTree($cat['id'], $group_id);
		}
		return $cats;
	}
	
	public function getCategoryInfo($id)
	{
		if (!$cat = Cache::load('category_'.$id))
		{
			if ($id == 0) return 0;

			$stmt = db::instance()->select('getCategoryInfo', array
			(
				'fields'	=> '*',
				'tables'	=> 'categories',
				'where'		=> array('id' => array('=', '?')),
				'limit'		=> 1
			));

			$stmt->execute(array($id));
			$cat = $stmt->fetch();
			Cache::save('category_'.$id, $cat);
		}

		return $cat;
	}

	public function getCategoryPermissions($category, $group)
	{
		if (!$permissions = Cache::load('category_permissions_'.$category.'_'.$group))
		{
			$stmt = db::instance()->select('getCategoryPermissions', array
			(
				'fields'	=> '*',
				'tables'	=> 'category_permissions',
				'where'		=> array('category_id' => array('=', '?'), 'group_id' => array('=', '?')),
				'limit'		=> 1
			));
			$stmt->execute(array($category, $group));
			$permissions = $stmt->fetch();
			Cache::save('category_permissions_'.$category.'_'.$group, $permissions);
		}

		return $permissions;
	}

	public function addCategory($parent_id = 0)
	{
		$db = db::instance();
		$list = array('name', 'description', 'parent_id', 'order');
		$stmt = $db->insert('addCategory', 'categories', $list);
		$stmt->execute(array(get_request('name', 'post'), get_request('description', 'post'), $parent_id, get_request('order', 'post')));
		$id = $db->lastInsertId();

		// don't forget to update cache
		if (Cache::check('category_children_'.$parent_id))
		{
			$cache = Cache::load('category_children_'.$parent_id);
			$cache[] = array(
							'id' => $id,
							'name' => get_request('name', 'post'),
							'description' => get_request('description', 'post'),
							'parent_id' => $parent_id,
							'order' => get_request('order', 'post')
			);
			Cache::save('category_children_'.$parent_id, $cache);
		}
		
		if (Cache::check('categories_list'))
		{
			$cache = Cache::load('categories_list');
			$cache[] = $id;
			Cache::save('categories_list', $cache);
		}

		// add permissions by group default
			$UserModel = new User_Model();

			// get goups
				$groups = $db->select('getGroupsPermissions', array
				(
					'fields'	=> '*',
					'tables'	=> 'group_permissions'
				), true)->fetchAll();
				//end

			$list = array('group_id', 'category_id', 'view', 'read', 'read_own_topics', 'create_topics', 'reply', 'reply_own_topics', 'edit', 'edit_own_posts', 'remove', 'remove_own_posts', 'remove_topics', 'remove_own_topics');
			$stmt = $db->insert('setPermissionsByDefault', 'category_permissions', $list);
			foreach ($groups as $group)
			{
				$stmt->execute(array($group['group_id'] ,$id, $group['cat_view'], $group['cat_read'], $group['cat_read_own_topics'], $group['cat_create_topics'], $group['cat_reply'], $group['cat_reply_own_topics'], $group['cat_edit'], $group['cat_edit_own_posts'], $group['cat_remove'], $group['cat_remove_own_posts'], $group['cat_remove_topics'], $group['cat_remove_own_topics']));
			}
			// end

		return $id;
	}

	public function updatePermissions($cat, $group)
	{
		$permissions = array(
					(is_numeric(get_request('view', 'post')) && get_request('view', 'post') > 0) ? 1 : 0,
					(is_numeric(get_request('read', 'post')) && get_request('read', 'post') > 0) ? 1 : 0,
					(is_numeric(get_request('read_own_topics', 'post')) && get_request('read_own_topics', 'post') > 0) ? 1 : 0,
					(is_numeric(get_request('create_topics', 'post')) && get_request('create_topics', 'post') > 0) ? 1 : 0,
					(is_numeric(get_request('reply', 'post')) && get_request('reply', 'post') > 0) ? 1 : 0,
					(is_numeric(get_request('reply_own_topics', 'post')) && get_request('reply_own_topics', 'post') > 0) ? 1 : 0,
					(is_numeric(get_request('edit', 'post')) && get_request('edit', 'post') > 0) ? 1 : 0,
					(is_numeric(get_request('edit_own_posts', 'post')) && get_request('edit_own_posts', 'post') > 0) ? 1 : 0,
					(is_numeric(get_request('remove', 'post')) && get_request('remove', 'post') > 0) ? 1 : 0,
					(is_numeric(get_request('remove_own_posts', 'post')) && get_request('remove_own_posts', 'post') > 0) ? 1 : 0,
					(is_numeric(get_request('remove_topics', 'post')) && get_request('remove_topics', 'post') > 0) ? 1 : 0,
					(is_numeric(get_request('remove_own_topics', 'post')) && get_request('remove_own_topics', 'post') > 0) ? 1 : 0,
					$cat, $group
		);

		$list = array('view', 'read', 'read_own_topics', 'create_topics', 'reply', 'reply_own_topics', 'edit', 'edit_own_posts', 'remove', 'remove_own_posts', 'remove_topics', 'remove_own_topics');
		$stmt = db::instance()->update('updateCategoryPermissions', 'category_permissions', $list, array('category_id' => array('=', '?'), 'group_id' => array('=', '?')));
		$stmt->execute($permissions);

		if (Cache::check('category_permissions_'.$cat.'_'.$group))
		{
			Cache::Remove('category_permissions_'.$cat.'_'.$group);
		}

		return true;
	}
	
	public function checkCategoryExist($id)
	{
		if (!Cache::check('categories_list'))
		{
			$_cats = array();
			$categories = db::instance()->select('getCategoriesList', array
			(
				'fields' => 'id',
				'tables' => 'categories'
			), true)->fetchAll();
			
			foreach ($categories as $category)
			{
				$_cats[] = $category['id'];
			}
			
			Cache::save('categories_list', $_cats);
		}

		$categories = Cache::load('categories_list');
		return (array_search($id, $categories) !== false) ? 1 : 0;
	}
}